﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using Microsoft.Office.Interop;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;
namespace Contacts
{   
    public static class func
    {
        public static DataTable origconpull { get; set; }
        public static DataTable origcompull { get; set; }
        public static DataRow fixdate { get; set; }
        public static DataRow update { get; set; }
        public static string button { get; set; }
        public static string num;        
        public static string removecharacters(string phone)        
        {
            StringBuilder sb = new StringBuilder();
            num = " ";
            for (int i = 0; i < phone.Length; i++)
            {
                if ((phone[i] >= '0' && phone[i] <= '9'))
                {sb.Append(phone[i]);}
            }
            phone = sb.ToString();
            return phone;
        }

        public static string area(string phone)
        {
            string p1 = phone.Substring(0, 3);
            return p1;
        }

        public static string prefix(string phone)
        {
            string p2 = phone.Substring(3, 3);
            return p2;
        }

        public static string line(string phone)
        {
            string p3 = phone.Substring(6, 4);
            return p3;
        }

        public static string ext(string phone)
        {
            string p4 = " ";
            if (phone.Length > 10)
            {
                p4 = phone.Substring(10, phone.Length - 10);
                return p4;
            }
            return p4;
        }

        public static bool d3(string digit)
        {
            string d = digit;
            int i;
            bool result = int.TryParse(d, out i);
            if (result = true && d.Length >= 3)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool d4(string digit)
        {
            string d = digit;
            int i;
            bool result = int.TryParse(d, out i);
            if (result = true && d.Length >= 4)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //this loads the datagridview with data
        public static void FillRows(DataSet dscont, DataGridView dgv)
        {
            
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                try
                {
                    string jr = Convert.ToString(dgv.Rows[i].Cells["job_role_id"].Value);
                    string c = Convert.ToString(dgv.Rows[i].Cells["contact_id"].Value);
                    if (!String.IsNullOrEmpty(jr) && !String.IsNullOrEmpty(c))
                    {
                        dgv.Rows[i].Cells[0].Value = dgv.Rows[i].Cells["job_role_id"].Value;
                        dgv.Rows[i].Cells[1].Value = dgv.Rows[i].Cells["contact_id"].Value;
                        object cv = dgv.Rows[i].Cells["contact_id"].Value;
                        DataRow conta = dscont.Tables["contacts"].Rows.Find(cv);
                        DataRow compa = dscont.Tables["companies"].Rows.Find(conta.ItemArray[5]);
                        int cat = (int)compa.ItemArray[9];
                        DataRow cata = dscont.Tables["categories"].Rows.Find(cat);
                        dgv.Rows[i].Cells[5].Value = conta.ItemArray[4];
                        dgv.Rows[i].Cells[6].Value = compa.ItemArray[1];
                        dgv.Rows[i].Cells[7].Value = compa.ItemArray[2];
                        dgv.Rows[i].Cells[8].Value = compa.ItemArray[3];
                        dgv.Rows[i].Cells[9].Value = compa.ItemArray[4];
                        dgv.Rows[i].Cells[10].Value = compa.ItemArray[5];
                        dgv.Rows[i].Cells[11].Value = compa.ItemArray[6];
                        dgv.Rows[i].Cells[12].Value = conta.ItemArray[6];
                        dgv.Rows[i].Cells[13].Value = compa.ItemArray[7];
                        dgv.Rows[i].Cells[14].Value = compa.ItemArray[8];
                        dgv.Rows[i].Cells[15].Value = conta.ItemArray[7];
                        dgv.Rows[i].Cells[16].Value = conta.ItemArray[8];
                        dgv.Rows[i].Cells[17].Value = cata.ItemArray[1];                        
                    }                    
                }

                catch (Exception e)
                {
                    MessageBox.Show("Here is the error " + e + "", "Why did you mess up?");
                }               
            }
        }

        public static void FillFolders(DataSet dscont, DataGridView dgv, int pi)
        {
            try
            {
                Outlook.Application ol = (Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application");
                Outlook.MAPIFolder fldr;
                Outlook.Folders strfldrs;
                Outlook.MAPIFolder strfldr;
                DataTable proj = dscont.Tables["projects"];
                DataTable projcont = dscont.Tables["project_contacts"];
                Outlook.Explorer olexp;
                Outlook.Items items;                
                DataRow[] filter;
                int strid;                
                
                    fldr = ol.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders).Folders["0001 Store Contacts"];
                    strfldrs = fldr.Folders;                    
                    
                    string str = "" + proj.Rows[pi]["str_city"] + " " + proj.Rows[pi]["str_st"] +
                        " " + proj.Rows[pi]["str_num"] + " - " + proj.Rows[pi]["str_seq"] + "";

                    olexp = ol.ActiveExplorer();
                    olexp.SelectFolder(strfldrs[str]);
                    strfldr = olexp.CurrentFolder;
                    items = strfldrs[str].Items;
                    strid = (int)proj.Rows[pi]["str_id"];

                    int ic = items.Count;
                    filter = projcont.Select(string.Format("str_id = {0}", strid));

                    if (ic > 0)
                    {
                        for (int j = ic; j > 0; j--)
                        {
                            items.Remove(j);
                        }
                    }

                    for (int k = 0; k < filter.Length; k++)
                    {
                        Outlook.ContactItem contitem;
                        contitem = strfldr.Items.Add("IPM.Contact") as Outlook.ContactItem;
                        contitem.MessageClass = "IPM.Contact.ProjectContactsv1";
                        contitem.UserProperties.Add("StoreNumber", Outlook.OlUserPropertyType.olText, true);
                        contitem.UserProperties.Add("StoreSeq", Outlook.OlUserPropertyType.olText, true);
                        contitem.UserProperties.Add("StoreCity", Outlook.OlUserPropertyType.olText, true);
                        contitem.UserProperties.Add("StoreState", Outlook.OlUserPropertyType.olText, true);
                        contitem.UserProperties.Add("JobRole", Outlook.OlUserPropertyType.olText, true);
                        contitem.UserProperties.Add("BusinessSuite", Outlook.OlUserPropertyType.olText, true);
                        contitem.UserProperties["StoreNumber"].Value = proj.Rows[pi]["str_num"];
                        contitem.UserProperties["StoreSeq"].Value = proj.Rows[pi]["str_seq"];
                        contitem.UserProperties["StoreCity"].Value = proj.Rows[pi]["str_city"];
                        contitem.UserProperties["StoreState"].Value = proj.Rows[pi]["str_st"];
                        DataRow jr = dscont.Tables["job_roles"].Rows.Find(filter[k].ItemArray[2].ToString());
                        contitem.UserProperties["JobRole"].Value = jr.ItemArray[1].ToString();
                        DataRow cont = dscont.Tables["contacts"].Rows.Find(filter[k].ItemArray[3].ToString());
                        contitem.FirstName = cont.ItemArray[1].ToString();
                        contitem.MiddleName = cont.ItemArray[2].ToString();
                        contitem.LastName = cont.ItemArray[3].ToString();
                        contitem.JobTitle = cont.ItemArray[4].ToString();
                        DataRow comp = dscont.Tables["companies"].Rows.Find(cont.ItemArray[5].ToString());
                        contitem.CompanyName = comp.ItemArray[1].ToString();
                        contitem.BusinessAddressStreet = comp.ItemArray[2].ToString();
                        contitem.UserProperties["BusinessSuite"].Value = comp.ItemArray[3].ToString();
                        contitem.BusinessAddressCity = comp.ItemArray[4].ToString();
                        contitem.BusinessAddressState = comp.ItemArray[5].ToString();
                        contitem.BusinessAddressPostalCode = comp.ItemArray[6].ToString();
                        contitem.BusinessTelephoneNumber = cont.ItemArray[6].ToString();
                        contitem.Business2TelephoneNumber = comp.ItemArray[7].ToString();
                        contitem.BusinessFaxNumber = comp.ItemArray[8].ToString();
                        contitem.MobileTelephoneNumber = cont.ItemArray[7].ToString();
                        contitem.Email1Address = cont.ItemArray[8].ToString();
                        DataRow cat = dscont.Tables["categories"].Rows.Find(comp.ItemArray[9].ToString());
                        contitem.Categories = cat.ItemArray[1].ToString();
                        contitem.Save();
                        Marshal.ReleaseComObject(contitem);
                        GC.Collect();
                    }                
            }
            catch (System.Exception e)
            {

            }
        }

        public static void FolderView(ProgressBar pb)
        {            
            Outlook.Application ol = (Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application");
            Outlook.MAPIFolder fldr;
            Outlook.Folders strfldrs;
            Outlook.View v;
            pb.Visible = true;
            fldr = ol.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders).Folders["0001 Store Contacts"];            
            strfldrs = fldr.Folders;
            int count = strfldrs.Count;
            pb.Minimum = 1;
            pb.Maximum = count;
            pb.Value = 1;
            pb.Step = 1;
            v = fldr.CurrentView;
            int f;

            for (f = 1; f < count + 1; f++)
            {
                int vcount = strfldrs[f].Views.Count;
                pb.PerformStep();

                int b;
                for (b = 1; b < vcount + 1; b++)
                {
                    if (strfldrs[f].Views[b].Name == "Project Contacts View")
                    { break; }
                }

                if (b == vcount + 1)
                {
                    v.Copy("Project Contacts View", Outlook.OlViewSaveOption.olViewSaveOptionAllFoldersOfType);
                    v.Save();
                }
                strfldrs[f].Views["Project Contacts View"].Apply();
            }
            pb.Value = 1;
            pb.Visible = false;
        }

        public static void OLcontacts()
        {}

        public static void cbocompsort(ComboBox cbocomp)
        {
            cbocomp.DisplayMember = "companies.comp_sel";
        }

        public static void returnkey(KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {}
        }

        public static void current_thread()
        {

        }

        public static void gettime(string when)
        {
            if (when == "top")
            {
                string top = DateTime.Now.ToLongTimeString();
            }
        }
    }
}
